﻿using System;
using System.Collections.Generic;
using LiSAClient.LiSAService;
using LiSAClient.ServiceProvider;

namespace LiSAClient.Core.Services
{
    public class UserService : BaseService
    {
        /// <summary>
        /// Retreives a user with the given ID
        /// </summary>
        /// <param name="id">ID of the suer to fetch</param>
        /// <returns>The user. If no user found, a new user</returns>
        public User GetUserByID(Guid id)
        {
            var user = new User();

            ServiceHandler.Instance.WithService(
                svc =>
                {
                    var userinfoquery = new UserInfoQuery
                    {
                        ID = id,
                    };
                    var result = (UserInfoQueryResult)svc.ExecuteQuery(userinfoquery);
                    if (result != null && result.User != null)
                    {
                        user = result.User;
                    }
                });
            return user;
        }

        /// <summary>
        /// Validates a potential new UserName
        /// </summary>
        /// <param name="potUserName">Name to check for availability</param>
        /// <returns>True if the name is free, false if not</returns>
        public bool ValidateUsername(string potUserName)
        {
            var isFree = false;

            ServiceHandler.Instance.WithService(
               svc =>
               {
                   var usernamequery = new UsernameQuery
                   {
                       UserID = Guid.Empty,
                       UserName = potUserName
                   };
                   var result = (UsernameQueryResult)svc.ExecuteQuery(usernamequery);
                   if (result != null)
                   {
                       isFree = !result.DoesUserExist;
                   }
               });
            return isFree;
        }

        /// <summary>
        /// Create a new User
        /// </summary>
        /// <param name="user"></param>
        public void CreateUser(User user)
        {
            ServiceHandler.Instance.WithService(
               svc =>
               {
                   var newUserCmd = new CreateUserCommand
                   {
                       User = user
                   };
                   svc.ExecuteCommand(newUserCmd);
               });
        }

        /// <summary>
        /// Gets all users in the system
        /// </summary>
        /// <returns>A list of users in the system</returns>
        public List<User> GetAllUsers()
        {
            var users = new List<User>();

            ServiceHandler.Instance.WithService(
                svc =>
                {
                    var userinfoquery = new AllUsersQuery();
                    var result = (AllUsersQueryResult)svc.ExecuteQuery(userinfoquery);
                    if (result != null && result.Users != null)
                    {
                        users = result.Users;
                    }
                });
            return users;
        }
    }
}
